package cn.com.xy.sms.sdk.util;

import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;

public class CrashHandler implements UncaughtExceptionHandler {
	private Thread.UncaughtExceptionHandler mDefaultHandler;
	private static CrashHandler s_Instance;

	public static CrashHandler getInstance() {
		if (s_Instance == null)
			s_Instance = new CrashHandler();
		return s_Instance;
	}

	/**
	 * init exception handler.
	 *
	 * @param context
	 */
	public void init(Context context) {
		mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		// TODO Auto-generated method stub
		handleException(thread, ex);
		mDefaultHandler.uncaughtException(thread, ex);
	}

	public boolean handleException(Thread thread, Throwable ex) {
		if (ex != null) {
			try {
				DexUtil.saveExceptionLog(ex);
			}
			catch (Exception e) {
				LogManager.e("CrashHandler", "handleException：", e);
			}
			return true;
		}
		return false;
	}
}
